/* *************************************************************************************************************/
/* ******************* Perform analysis of calendar-time portfolio returns of mutual funds  ********************/
/* *************************************************************************************************************/

proc sql;
  create table measure2 as
  select a.*, b.caldt, b.mret, b.gret, b.mret_style_adj, b.mtna_pr1
  from temp.measure_fund_qtr (drop=caldt) as a left join temp.fundret_monthly as b
  on a.wficn = b.wficn and a.crsp_cl_grp = b.crsp_cl_grp and intck('quarter', a.qtr, b.caldt) in (1);
quit; * temp.measure_fund_qtr is from 17Trade_port_performance.sas;

%winsor(dsetin=measure2, dsetout=measure2, byvar=none, vars=mret gret mret_style_adj mtna_pr1, type=winsor, pctl=1 99);


*** Vote Alpha (CAR) ----------- Value-weighting;
proc sort data = measure2; by informed caldt;

proc means data = measure2 noprint; by informed caldt;
  var mret gret mret_style_adj; weight mtna_pr1; where mtna_pr1 > .; 
  output out = cal_time_ret1a (drop=_type_ _freq_) mean = mret gret mret_style_adj; 
run;

proc means data = measure2 noprint; by informed caldt;
  var mret gret mret_style_adj; where mtna_pr1 > .;
  output out = cal_time_ret1b (drop=_type_ _freq_) n = num_funds;
run;

data cal_time_ret; merge cal_time_ret1a cal_time_ret1b; by informed caldt;

proc sql;
  create table cal_time_ret as
  select a.*, b.hml, b.smb, b.mktrf, b.umd, b.rf, a.gret-b.rf as gretx, a.mret-b.rf as mretx  
  from cal_time_ret as a, ff.factors_monthly as b
  where year(a.caldt) = year(b.dateff) and month(a.caldt) = month(b.dateff);
quit;

proc sql;
  create table cal_time_ret as
  select a.*, b.ps_vwf 
  from cal_time_ret as a, ff.liq_ps as b
  where year(a.caldt) = year(b.date) and month(a.caldt) = month(b.date);
quit;

proc sql;
  create table cal_time_ret2 as
  select a.*, a.mret-b.mret as diff, a.gret-b.gret as diff_g
  from cal_time_ret as a, cal_time_ret as b
  where a.caldt = b.caldt and a.informed = 1 and b.informed = 0;
quit;

proc reg data = cal_time_ret; by informed; 
  model gretx = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret2; 
  model diff_g = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret; by informed; 
  model mretx = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret2; 
  model diff = mktrf smb hml umd; 
quit; run;




*** Vote Alpha (CAR) ----------- Equal-weighting;
proc sort data = measure2; by informed caldt;

proc means data = measure2 noprint; by informed caldt;
  var mret gret mret_style_adj; where mtna_pr1 > .; 
  output out = cal_time_ret1a (drop=_type_ _freq_) mean = mret gret mret_style_adj; 
run;

proc means data = measure2 noprint; by informed caldt;
  var mret gret mret_style_adj; 
  output out = cal_time_ret1b (drop=_type_ _freq_) n = num_funds;
run;

data cal_time_ret; merge cal_time_ret1a cal_time_ret1b; by informed caldt;

proc sql;
  create table cal_time_ret as
  select a.*, b.hml, b.smb, b.mktrf, b.umd, b.rf, a.gret-b.rf as gretx, a.mret-b.rf as mretx, a.mret-b.mktrf as mret_capm 
  from cal_time_ret as a, ff.factors_monthly as b
  where year(a.caldt) = year(b.dateff) and month(a.caldt) = month(b.dateff);
quit;

proc sort; by informed;

proc sql;
  create table cal_time_ret2 as
  select a.*, a.mret-b.mret as diff, a.gret-b.gret as diff_g
  from cal_time_ret as a, cal_time_ret as b
  where a.caldt = b.caldt and a.informed = 1 and b.informed = 0;
quit;

proc reg data = cal_time_ret; by informed; 
  model gret = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret2; 
  model diff_g = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret; by informed; 
  model mretx = mktrf smb hml umd; 
quit; run;

proc reg data = cal_time_ret2; 
  model diff = mktrf smb hml umd; 
quit; run;






